package com.tencent.cloud.iov.action;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * IActionListener 组合
 *
 * @author showxiao
 * @date 2018/6/26
 */
public class CompositeActionListener implements IActionListener {

    private List<IActionListener> mActionListeners;

    public CompositeActionListener() {

    }

    public void add(IActionListener listener) {
        if (mActionListeners == null) {
            mActionListeners = new CopyOnWriteArrayList<>();
        }
        mActionListeners.add(listener);
    }

    public void remove(IActionListener listener) {
        if (mActionListeners != null) {
            mActionListeners.remove(listener);
        }
    }

    public void clear() {
        if (mActionListeners != null) {
            mActionListeners.clear();
        }
    }

    @Override
    public boolean onAction(String actionId, Object... args) {
        if (mActionListeners != null && !mActionListeners.isEmpty()) {
            for (IActionListener listener : mActionListeners) {
                boolean handled = listener.onAction(actionId, args);
                if (handled) {
                    return true;
                }
            }
        }

        return false;
    }
}
